package com.yonyou.cyxdms.sysAuth.service.controller.dealer;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.constants.SysAuthDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.dealer.MsgRecordDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.dealer.MsgRecordPO;
import com.yonyou.cyxdms.sysAuth.service.dto.dealerchannel.TmDealerchannelDTO;
import com.yonyou.cyxdms.sysAuth.service.mapper.MsgRecordMapper;
import com.yonyou.cyxdms.sysAuth.service.service.dealerchannel.MsgRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;


/**
 * @author duxiaoguang
 * @since 2019-09-04
 */
@Api(value = "通知列表", tags = {"通知列表"})
@RestController
@RequestMapping("/dealer/msgRecord")
public class MsgRecordController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(MsgRecordController.class);

    @Autowired
    public MsgRecordService msgRecordService;

    @Autowired
    MsgRecordMapper msgRecordMapper;

    /**
     * 分页查询数据
     *
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage 分页对象
     * @author duxiaoguang
     * @since 2019-09-04
     */
    @ResponseBody
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(value = "员工id", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "分页查询通知消息列表")
    public IPage<Map<String, Object>> getList(Map<String, Object> queryCondition, @RequestParam
            ("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        Page<Map<String, Object>> page = new Page(currentPage, pageSize);
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        queryCondition.put("userId", loginInfoDto.getUserId());
        return msgRecordService.selectPageBysql(page, queryCondition);
    }

    @PutMapping(value = "tz/{msgStatus}")
    @ApiOperation(value = "通知-已读状态")
    public void editDealerChannel(@PathVariable Integer msgStatus) {
        msgRecordMapper.updateNotices(msgStatus);
    }


    /**
     * PC-通知查看
     *
     * @param currentPage
     * @param pageSize
     * @return
     * @author duxiaoguang
     */
    @GetMapping("/inForm")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "员工id", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "通知查看—PC")
    public IPage<Map<String, Object>> selectPageByTz(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, String msgType, String msgContent, String beginTime, String endTime) {
        Page<Map<String, Object>> page = new Page(currentPage, pageSize);
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Map<String, Object> queryCondition = new HashMap<>();
        queryCondition.put("userId", loginInfoDto.getUserId());
        if (!StringUtils.isNullOrEmpty(msgType)) {
            queryCondition.put("msgType", msgType.split(","));
        }
        queryCondition.put("msgContent", msgContent);
        queryCondition.put("createdAt", beginTime);
        queryCondition.put("endTime", endTime);


//        Page<MsgRecordDTO> ipage = new Page(currentPage, pageSize);
//        List<MsgRecordDTO> msgList = msgRecordMapper.selectPageByTz(ipage, queryCondition);
//        for (MsgRecordDTO dto : msgList) {
//            if (70281040 == dto.getTemplateCode()) {
//                MsgRecordPO po = msgRecordMapper.selectById(dto.getMsgId());
//                po.setMsgStatus(ManageDictCodeConstants.RELEASE_STATUS_YES);
//                msgRecordMapper.updateById(po);
//            }
//        }

        return msgRecordService.selectPageByTzs(page, queryCondition);
    }

    /**
     * 查询通知类型（审核，批准，转发）
     * adu
     */
    @GetMapping("/selectMsgType")
    @ApiOperation(value = "通知类型")
    public List<Map<String, String>> selectMsgType() {
        return msgRecordService.selectMsgType();
    }


    /**
     * 手机端-清空（修改状态）
     *
     * @author duxiaoguang
     * @since 2019-09-04
     */
    @DeleteMapping("/deleteAll")
    @ResponseStatus(HttpStatus.OK)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "员工id", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "清空通知")
    public Integer deleteById() {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        return msgRecordService.deleteById(loginInfoDto.getUserId().intValue());

    }


    /**
     * 进行数据修改
     *
     * @param id 数据主键ID
     * @return Map<String, Object> 数据对应的map 结构
     * @author duxiaoguang
     * @since 2019-09-04
     */
   /* @ResponseBody
    @GetMapping(value = "/{id}")
    public Map<String, Object> getById(@PathVariable("id") Integer id) {
        return msgRecordService.getById(id);
    }
*/
    /**
     * 进行数据新增
     *
     * @param msgRecordDTO 需要保存的DTO
     * @return Map<String, Object> 返回的数据map
     * @author duxiaoguang
     * @since 2019-09-04
     */
/*    @ResponseBody
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "员工id", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "添加通知消息列表")
    public Map<String, Object> insert(@RequestBody MsgRecordDTO msgRecordDTO) {
        logger.debug("msgRecordDTO:{}", msgRecordDTO);
        return msgRecordService.insert(msgRecordDTO);
    }*/

    /**
     * 进行数据修改
     *
     * @param id           需要修改数据的ID
     * @param msgRecordDTO 需要保存的DTO
     * @return Map<String, Object> 返回的数据map
     * @author duxiaoguang
     * @since 2019-09-04
     */
   /* @ResponseBody
    @PutMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    @SuppressWarnings("unchecked")
    public Map<String, Object> update(@PathVariable("id") Integer id, @RequestBody MsgRecordDTO msgRecordDTO) {
        logger.debug("msgRecordDTO:{}", msgRecordDTO);
        return msgRecordService.update(id, msgRecordDTO);
    }*/


    /**
     * 根据IDs批量删除对象
     *
     * @param ids 需要修改数据的ID集合
     * @author duxiaoguang
     * @since 2019-09-04
     */
   /* @DeleteMapping(value = "/batch/{ids}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public boolean deleteByIds(@PathVariable("ids") String ids) {
        logger.debug("删除ID:{}", ids);
        msgRecordService.deleteBatchIds(ids);
        return true;
    }
*/

}